/**
 * 
 */
package org.vsg.stock.basic.persistence.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.stock.core.domain.StockTick;

/**
 * @author vison
 *
 */
public class ZipReadTickFileProcessor implements ReadTickFileProcessor {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private SimpleDateFormat dftime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	
	private static final Logger logger = LoggerFactory.getLogger(ZipReadTickFileProcessor.class);
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.persistence.file.ReadTickFileProcessor#readData(java.io.File, java.lang.String, java.util.Date)
	 */
	@Override
	public List<StockTick> readData(File parentFolder, String stockCode,
			Date date) throws FileNotFoundException {
		// TODO Auto-generated method stub
		logger.debug("reade tick data ..... ");		
		File dataFile = new File(parentFolder, sdf.format(date)+".zip");

		ZipFile zipFile = null;
		
		List<StockTick> allStockTicks = new LinkedList<StockTick>();
		
		try {
			// --- init processor ----
			
			zipFile = new ZipFile( dataFile ) ;
			
			ZipEntry zipEntry = null;
			
			InputStream is = null;
			for (Enumeration enumer = zipFile.entries(); enumer.hasMoreElements() ; ) {
				zipEntry = (ZipEntry)enumer.nextElement();
				
				try {
					String fileMainName = zipEntry.getName().replaceFirst(".txt", "");
					
					if ( stockCode.equalsIgnoreCase(fileMainName) ) {
						is = new BufferedInputStream( zipFile.getInputStream( zipEntry ) ) ;
						
						List<String> dataLines = IOUtils.readLines(is);
						String line = null;
						StockTick stkTick = null;
						for (Iterator<String> linesIter = dataLines.iterator() ; linesIter.hasNext();) {
							line = linesIter.next();
							
							try {
								if (!line.trim().equals("")) {
									String[] sprLine = line.split(",");
									stkTick = new StockTick();
									stkTick.setCode( sprLine[0] );
									stkTick.setDateTime( dftime.parse( sprLine[1] ) );
									stkTick.setLastCurrentPrice( Double.parseDouble( sprLine[2] ) );
									stkTick.setSumTick( (int)Double.parseDouble(sprLine[3] ) );
									stkTick.setAmountVol( Double.parseDouble( sprLine[4] ) );
									stkTick.setPresentTickVol( Integer.parseInt( sprLine[5] ) );
									
									stkTick.setTickType( sprLine[6]);
									
									stkTick.setConsignedBuy1Price( Double.parseDouble( sprLine[7] ) );
									stkTick.setConsignedBuy1Vol( Long.parseLong( sprLine[8] ) );
									stkTick.setConsignedBuy2Price( Double.parseDouble( sprLine[9] ) );
									stkTick.setConsignedBuy2Vol( Long.parseLong( sprLine[10] ) );
									stkTick.setConsignedBuy3Price( Double.parseDouble( sprLine[11] ) );
									stkTick.setConsignedBuy3Vol( Long.parseLong( sprLine[12] ) );
									stkTick.setConsignedBuy4Price( Double.parseDouble( sprLine[13] ) );
									stkTick.setConsignedBuy4Vol( Long.parseLong( sprLine[14] ) );
									stkTick.setConsignedBuy5Price( Double.parseDouble( sprLine[15] ) );
									stkTick.setConsignedBuy5Vol( Long.parseLong( sprLine[16] ) );									

									stkTick.setConsignedSell1Price( Double.parseDouble(  sprLine[17] ) );
									stkTick.setConsignedSell1Vol( Long.parseLong( sprLine[18] ));
									stkTick.setConsignedSell2Price( Double.parseDouble(  sprLine[19] ) );
									stkTick.setConsignedSell2Vol( Long.parseLong( sprLine[20] ));
									stkTick.setConsignedSell3Price( Double.parseDouble(  sprLine[21] ) );
									stkTick.setConsignedSell3Vol( Long.parseLong( sprLine[22] ));
									stkTick.setConsignedSell4Price( Double.parseDouble(  sprLine[23] ) );
									stkTick.setConsignedSell4Vol( Long.parseLong( sprLine[24] ));
									stkTick.setConsignedSell5Price( Double.parseDouble(  sprLine[25] ) );
									stkTick.setConsignedSell5Vol( Long.parseLong( sprLine[26] ));
									stkTick.setTotalTick( Integer.parseInt( sprLine[27] ) );
									allStockTicks.add( stkTick );
									
								}
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						

						break;
					}
					
				}  finally{
					if (is != null) {
						try {
							is.close();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} finally {
							is = null;
						}
					}
				}
			}
			
			
		} catch (ZipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			
			try {
				if (zipFile != null) {
					zipFile.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				
				e.printStackTrace();
			
			}
		}	
		
		return allStockTicks;
	}

	@Override
	public boolean checkFileExist(File parentFolder, String stockCode, Date date) {
		// TODO Auto-generated method stub
		File dataFile = new File(parentFolder, sdf.format(date)+".zip");
		return dataFile.exists();
	}

	
	
}
